import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Query,
  Delete,
  BadRequestException,
} from '@nestjs/common';
import { TextService } from './text.service';
import { CreateTextDto } from './dto/create-text.dto';
import { UpdateTextDto } from './dto/update-text.dto';
import { TextEntity } from './entities/text.entity';
@Controller('text')
export class TextController {
  constructor(private readonly textService: TextService) {}

  @Post()
  create(@Body() createTextDto: CreateTextDto) {
    return this.textService.create(createTextDto);
  }

  //获取所有文本列表
  @Get('lists')
  async findAll(
    @Query('pageNum') pageNum: number = 1,
    @Query('pageSize') pageSize: number = 10,
  ): Promise<{
    code: number;
    data: TextEntity[];
    total: number;
  }> {
    return this.textService.findAll(pageNum, pageSize);
  }

  //获取具体书籍的文本列表
  @Get('list') //http://localhost:3000/api/text/list?bid=1
  async findOne(
    @Query('bid') bid: number,
    @Query('pageNum') pageNum: number = 1,
    @Query('pageSize') pageSize: number = 10,
  ): Promise<{
    code: number;
    data: TextEntity[];
    total: number;
  }> {
    if (!bid) {
      throw new BadRequestException('缺少必填参数: bid');
    }
    return this.textService.findOne(bid, pageNum, pageSize);
  }

  // @Get('list/:id') //http://localhost:3000/api/text/list/1
  // findOne(@Param('id') id: string) {
  //   return this.textService.findOne(+id);
  // }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateTextDto: UpdateTextDto) {
    return this.textService.update(+id, updateTextDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.textService.remove(+id);
  }
}
